package org.streets.eis.ext.analysis.pages;

import java.util.ArrayList;
import java.util.List;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.wicket.PageParameters;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.streets.database.datadict.ColumnDefinition;
import org.streets.database.datadict.DataDict;
import org.streets.database.datadict.TableDefinition;
import org.streets.eis.EisWebSession;
import org.streets.eis.component.messagebox.MessageBox.ButtonType;
import org.streets.eis.component.messagebox.MessageBox.IconType;
import org.streets.eis.entity.User;
import org.streets.eis.ext.analysis.components.CheckItem;
import org.streets.eis.ext.analysis.components.ColumnWrapper;
import org.streets.eis.ext.analysis.entities.DisplayField;
import org.streets.eis.ext.analysis.entities.Field;
import org.streets.eis.ext.analysis.entities.OrderField;
import org.streets.eis.ext.analysis.entities.QueryStruct;
import org.streets.eis.ext.analysis.entities.WhereField;
import org.streets.eis.ext.analysis.internal.buildlogic.QueryBuilderLogic;
import org.streets.eis.ext.analysis.services.QueryService;
import org.streets.eis.pages.BasePage;
import com.sophia.hr.modules.examples.pages.DefaultHeaderPanel;

 
public class QueryPage extends BasePage {
	
	@Inject
	protected QueryBuilderLogic builder;
	@Inject
	protected QueryService queryService;
	
	@Inject
	protected DataDict dict;
	
	protected User user;
	
	protected QueryStruct currentQuery;//选择的查询
	
	
	 public QueryPage() {
	        super(); 
	        add(new Label("htmlTitle",""));
	        add(new DefaultHeaderPanel("header"));
	        user = ((EisWebSession)Session.get()).getUser();
	 }
	    
	 public QueryPage(PageParameters params) {
	    	super(params);
	    	add(new Label("htmlTitle",""));
	    	add(new DefaultHeaderPanel("header"));
	    	user = ((EisWebSession)Session.get()).getUser();
	 }
	 
	 public QueryPage(IModel<?> model) {
	        super(model);
	        add(new Label("htmlTitle",""));
	        add(new DefaultHeaderPanel("header"));
	        user = ((EisWebSession)Session.get()).getUser();
	 }
	
	 public void showInfo(String msg){
		 getMessageBox().show("信息", msg, IconType.INFO,
					ButtonType.OK, null, -1, -1); 
	 }
	 public void showError(String msg){
		 getMessageBox().show("错误", msg, IconType.ERROR,
					ButtonType.OK, null, -1, -1); 
	 }
	 public void showHint(String msg){
		 getMessageBox().show("提示", msg, IconType.INFO,
					ButtonType.OK, null, -1, -1); 
	 }

 
	public List<ColumnWrapper> loadWrapperedFields(TableDefinition table){
		List<ColumnWrapper> list = new ArrayList<ColumnWrapper>();
		
		for(ColumnDefinition def : table.getColumns()){
			ColumnWrapper w = new ColumnWrapper(def);
			setDisplayProperty(w);
			setIfclauseProperty(w);
			setOrderProperty(w);	
			
			list.add(w);
		}
		
		return list;
	}

	private void setDisplayProperty(ColumnWrapper w){
	if (currentQuery != null){
		for(DisplayField f:currentQuery.getDisplayFields()){
			if (isSameField(f,w)){
				w.setDisplay(true);
				break;
			}
		}
	}
	}
	
	private void setIfclauseProperty(ColumnWrapper w){
	if (currentQuery != null){
		for(WhereField f:currentQuery.getWhereFields()){
			if (isSameField(f,w)){
				w.setIfclause(true);
				break;
			}
		}
	}
	}

	private void setOrderProperty(ColumnWrapper w){
		if (currentQuery != null){
			for(OrderField f:currentQuery.getOrderFields()){
			if (isSameField(f,w)){
				w.setOrder(true);
				break;
			}				
		}
	}
	}
	
	
	
	private boolean isSameField(Field f,ColumnWrapper w){
		if ((isSameTableName(f,w)) && (isSameFieldName(f,w))){
			return true;
		}else {
			return false;
		}
	}
				
	private boolean isSameTableName(Field f,ColumnWrapper w){
		return f.getTableCode().equalsIgnoreCase(w.getTable().getName());
	}
	private boolean isSameFieldName(Field f,ColumnWrapper w){
		return f.getFieldCode().equalsIgnoreCase(w.getName());
	}
	
	
	//所有表
    public IModel<List<TableDefinition>> getTablesModel(){
    	return new LoadableDetachableModel<List<TableDefinition>>(){
    		private static final long serialVersionUID = 1L;
    			public List<TableDefinition> load() {
    				List<TableDefinition> list = dict.getTableDefs();
    				return list;
    			}	
    	};
    }
    
    //所有查询
    public IModel<List<QueryStruct>> getQueriesModel(){
    	return new LoadableDetachableModel<List<QueryStruct>>(){
    		private static final long serialVersionUID = 1L;
    		public List<QueryStruct> load() {
    			List<QueryStruct> list = builder.findQueries();
			    return list;
			 }	
    	};
    }

	public void setResult(CheckItem item) {
	 
		
	}
}
